<?php
class Admin_ArticleController extends My_Controller_Admin_Action
{
	protected $articleModel = '';

	protected function _init()
	{
		$this->articleModel = new Admin_Model_Article();
	}

	public function indexAction()
	{
		$rowPerPage = $this->_getParam('rowperpage', 20);
		$currentPage = 1;
		$page = $this->_getParam('page', 1);
		if(!empty($page)) {
			$currentPage = $page;
		}

		$rows = $this->articleModel->getListArticle();
		$paginator = Zend_Paginator::factory($rows);
		$paginator->setItemCountPerPage($rowPerPage);
		$paginator->setCurrentPageNumber($currentPage);

		$this->view->page = $page;
		$this->view->paginator = $paginator;

	}

	public function addArticleAction()
	{
		$this->_forward('edit');
	}

	public function editAction()
	{
	    $id = $this->_getParam('id', null);
	    $data = array();
	    if ($id) {
	        $data = $this->articleModel->getArticle($id);
	    }
	    $this->view->object = new My_Object($data);
	}

	public function saveAction()
	{
        $data = $this->_getParam('data');
        $id = $this->_getParam('id', null);
        $conserve = $this->_getParam('conserve');
        $error = false;
        $data['dtpubli'] = My_Date::fomatDbDate($data['dtpubli']);
        if ($conserve) {
            if (isset($conserve['fichier']) && $conserve['fichier'] != 1) {
                Admin_Model_Upload::remove($conserve['fichier']);
                $data['fichier'] = '';
            }
            if (isset($conserve['vignette']) && $conserve['vignette'] != 1) {
                Admin_Model_Upload::remove($conserve['vignette']);
                $data['vignette'] = '';
            }
        }

	    if ($_FILES['vignette']['tmp_name']) {
	        try {
    	        $result = Admin_Model_Upload::upload('vignette', array('jpg', 'png', 'gif'));
    	        $data['vignette'] = $result['file'];
	        } catch (Exception $e) {
	            $flashMessenger = $this->getHelper('flashMessenger');
	            $flashMessenger->setNamespace('error')->addMessage($e->getMessage() . ' for Vignette');
	            $error = true;
	        }
	    }
	    if ($_FILES['fichier']['tmp_name']) {
	        try {
	            $result = Admin_Model_Upload::upload('fichier');
	            $data['fichier'] = $result['file'];
	        } catch (Exception $e) {
	            $flashMessenger = $this->getHelper('flashMessenger');
	            $flashMessenger->setNamespace('error')->addMessage($e->getMessage() . ' for Fichier');
	            $error = true;
	        }
	    }

        if (!$error) {
            $this->articleModel->save($data, $id);
            $this->_redirect('admin/article');
        }
        $this->_redirect('admin/article/edit/id/' . $id);
	}

	public function deleteAction()
	{
		$id = $this->_getParam('id');
		$page = $this->_getParam('page', 1);
		$articleModel = new Admin_Model_Article();
		$articleModel->removeArticle($id);

		$param = '';
		if($page > 1) $param = 'index/page/' . $page;
		$this->_redirect('admin/article/' . $param);
		exit;
	}
}

